from flask import jsonify,current_app
from json import dumps

def view_response(code=200,msg='操作参观',error=None,data=None):
    indent = None
    separators = (",", ":")

    if current_app.config["JSONIFY_PRETTYPRINT_REGULAR"] or current_app.debug:
        indent = 2
        separators = (", ", ": ")
    dic = {
        'code':code,
        'msg':msg
    }
    if code==200:
        if data:
            dic['data']=data
    elif code==400:
        dic.pop('msg')
        dic['error']=error if error else '报错了，没有写原因'
    else:
        dic.pop('msg')
        dic['error'] = '系统内部错误'

    return current_app.response_class(
        dumps(dic, indent=indent, separators=separators) + "\n",
        mimetype=current_app.config["JSONIFY_MIMETYPE"],
    )

